let express = require('express');
let router = express.Router();

// 引入jwt验证方法的中间件
const checkJWTMiddleWare = require('../middleWare/checkJWTMiddleWare');
router.use(checkJWTMiddleWare);

const focuscardService = require('../service/focuscardService');

/*
 * 专注模式打卡功能
 */
router.post('/focuscard', async function(req, res, next) {
    let time = req.formatTime;
    let uid = req.jwtResult.data.uid;
    let duration = req.body.duration;
    if (!duration) {
        res.status(400).json({ msg: '参数错误' });
        return;
    }
    let result = await focuscardService.focuscard(uid, time, duration);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 打卡记录统计接口
 * 返回：总专注时长、日均专注时长、连续打卡几天
 */
router.get('/checkinStatistics', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let result = await focuscardService.checkinStatistics(uid);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 排行榜接口
 * 返回：前100的名次
 */
router.get('/checkinRanking', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let result = await focuscardService.checkinRanking(uid);
    res.status(result.success ? 200 : 400).json(result);
});

/*
 * 打卡记录趋势接口
 * 返回：最近7天的打卡记录
 */
router.get('/checkinTrend', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let result = await focuscardService.checkinTrend(uid);
    res.status(result.success ? 200 : 400).json(result);
});

// 分页获取所有专注打卡模式记录
router.get('/getAllFocuscard', async function(req, res, next) {
    let uid = req.jwtResult.data.uid;
    let page = parseInt(req.query.page || 1);
    let result = await focuscardService.getAllFocuscard(uid, page);
    res.status(result.success ? 200 : 400).json(result);
});

module.exports = router;
